//
//  GDUserLoginController.m
//  GraduationDesign
//
//  Created by JiaKang.Zhong on 16/1/3.
//  Copyright © 2016年 JiaKang.Zhong. All rights reserved.
//

#import "GDUserLoginController.h"
#import "GDUserRegisterController.h"
#import "GDChooseControllerOperation.h"
#import "GDUserAccountModel.h"
#import "RegViewController.h"
#import "GDUserAccountModel.h"
#import "MBProgressHUD+MJ.h"
#import <BmobSDK/Bmob.h>
#import <UIImageView+WebCache.h>
@interface GDUserLoginController ()
@property (weak, nonatomic) IBOutlet UITextField *txtUserName;
@property (weak, nonatomic) IBOutlet UITextField *txtUserPwd;
@property (weak, nonatomic) IBOutlet UIImageView *imageIconView;
@property (weak, nonatomic) IBOutlet UILabel *lblNeedRegister;

@property (weak, nonatomic) IBOutlet UILabel *lblForgetPwd;
- (IBAction)userLoginClick:(id)sender;
@end

@implementation GDUserLoginController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    UITapGestureRecognizer * tapGestureForView = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(shutDown)];
    [self.view addGestureRecognizer:tapGestureForView];

    NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
    _txtUserName.text = [defaults objectForKey:@"userName"];
  
    //更改cell里的图片
    self.imageIconView.layer.masksToBounds = YES;
    self.imageIconView.layer.cornerRadius = 40;
    self.imageIconView.layer.borderWidth = 0.5;
    self.imageIconView.layer.borderColor = [UIColor whiteColor].CGColor;
    [self.imageIconView sd_setImageWithURL:[NSURL URLWithString:[defaults objectForKey:[NSString stringWithFormat:@"%@url", _txtUserName.text]]] placeholderImage:[UIImage imageNamed:@"placeholder_login"]];

}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    

}

//- (void) showUserIcon :(NSData *)iconData{
//    CGFloat imageX = GDScreenSize.size.width * 0.5 - self.imageIconView.frame.size.width * 0.5;
//    CGFloat imageY = self.txtUserName.frame.size.height + 20 + self.imageIconView.frame.size.height * 0.5;
//    
//    UIImageView * imageViewIcon = [[UIImageView alloc ]initWithFrame:(CGRect){{imageX,imageY},self.imageIconView.frame.size}];
//    imageViewIcon.layer.masksToBounds = YES;
//    imageViewIcon.layer.cornerRadius = 100;
//    // 显示用户头像
//    imageViewIcon.image  = [UIImage imageWithData:iconData];
//    [self.imageIconView removeFromSuperview];
//    [self.view addSubview:imageViewIcon];
//    
//    
//    
//}

#pragma mark 关闭键盘
-(void) shutDown{
    [self.view endEditing:YES];
}

-(void) viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    UIColor *color = [UIColor whiteColor];
    
    [self.txtUserName setValue:color forKeyPath:@"_placeholderLabel.textColor"];//修改颜色
    [self.txtUserPwd setValue:color forKeyPath:@"_placeholderLabel.textColor"];//修改颜色
    self.lblForgetPwd.userInteractionEnabled = YES;
    self.lblNeedRegister.userInteractionEnabled = YES;
    
    UITapGestureRecognizer * tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(registerClick:)];
    [self.lblForgetPwd addGestureRecognizer:tapGes];
    UITapGestureRecognizer * needRegisterTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(registerClick:)];
    [self.lblNeedRegister addGestureRecognizer:needRegisterTapGes];
   
   
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark 登录触发
- (IBAction)userLoginClick:(id)sender {
    [MBProgressHUD showMessage:@"正在登录中..."];
    NSString * userName = self.txtUserName.text;
    NSString * userPwd  = self.txtUserPwd.text;
//    GDUserAccountModel * model = [GDUserAccountModel shareInstance];
    //查找GDUserAccount表
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"GDUserAccount"];
    
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        [MBProgressHUD hideHUD];
        if (array.count == 0) {
            ShowErrorAlert(@"用户名不存在");
            return;
        }
        BOOL isResult = false;
        for (BmobObject *object in array) {
            if ([[object objectForKey:@"UserName"] isEqualToString:userName] && [[object objectForKey:@"UserPwd"] isEqualToString:userPwd]) {
                NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:userName forKey:@"userName"];
                [defaults synchronize];
                
                // 登录成功
                [GDChooseControllerOperation chooseController:DGKeywindow];
                isResult = true;
            }
            
        }
        if (!isResult) {
          ShowErrorAlert(@"用户名不存在或者密码错误");
        }
    }];


    
}

-(BOOL)prefersStatusBarHidden{
    return YES;
}




- (void)registerClick:(UITapGestureRecognizer *)tapGes {

    if (tapGes.view.tag) {
        #pragma mark 注册用户
            GDUserRegisterController * registerController = [[GDUserRegisterController alloc] init];
            [self presentViewController:registerController animated:YES completion:nil];
    }else{
        #pragma mark 找回密码
        RegViewController * regViewController = [[RegViewController alloc]init];
        [self presentViewController:regViewController animated:YES completion:nil];
        
    }
    
}
@end
